package com.skyworth.web.admin.store.controller;

import com.skyworth.web.admin.store.form.StoreForm;
import com.skyworth.web.admin.store.model.StoreModel;
import com.skyworth.web.admin.store.service.StoreService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.Constants;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.MenuCss;
import com.skyworth.web.common.form.Paginator;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by skyworth on 2016/8/11.
 */
@Controller
@RequiresRoles(value={Constants.STORE_ROLE})
@RequestMapping("/admin/store")
public class StoreController extends BaseController {

	Logger LOG = LoggerFactory.getLogger(StoreController.class);

	@Autowired
	private StoreService storeService;

	@Autowired
	private MenuCss menuCss;

	@RequiresRoles(value={Constants.STORE_ROLE})
	@RequestMapping("")
	public String index(Paginator paginator , Model model) {
		UserModel userModel = currentUser();
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouStore = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		Page<StoreModel> page = storeService.storePage(null, paginator.pageRequest());
		paginator.feedToModel(page,model);
		return "admin/store/stores";
	}


	@RequestMapping(value = {"mine/{id}"}, method = RequestMethod.GET)
	public String mystore(@PathVariable("id") long id, Model model) {
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouStore = "hover";
		StoreModel storeModel = storeService.findStore(id);
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("store", storeModel);
		model.addAttribute("user", currentUser());
		return "admin/store/mystore";
	}

	@RequestMapping(value = {"{id}"}, method = RequestMethod.GET)
	public String edit(@PathVariable("id") long id, Model model) {
		UserModel userModel = currentUser();
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouStore = "hover";
		StoreModel storeModel = storeService.findStore(id);
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		model.addAttribute("store", storeModel);
		return "admin/store/store_edit";
	}

	@RequestMapping(value = {"{id}"}, method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
						 @ModelAttribute StoreForm storeForm,
						 Model model) {
		Map<String, Object> map = new HashMap<String, Object>();

		StoreModel storeModel = storeService.updateStore(storeForm);

		model.addAttribute("store", storeModel);
		UserModel userModel = currentUser();
		map.put("valid", true);

		map.put("url", "/admin/store/mine/"+userModel.storeId);

		return map;
	}

	@RequestMapping(value = {"{id}"}, method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String,Object> delete(@PathVariable("id") long id,
									 HttpSession session) {
		Map<String, Object> rsp = responseMap();
		StoreModel storeModel = storeService.findStore(id);

		if (storeModel != null) {
			storeService.removeStore(id);
			rsp.put("success", true);
		}else{
			rsp.put("success", false);
			rsp.put("message", "store doesn't exist");
		}
		return rsp;
	}
}
